package cn.kgc.jdktest.demo10;

import com.google.common.base.Charsets;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.springframework.context.annotation.Bean;

import java.util.List;
import java.util.logging.Logger;

/**
 * @Author:yaozhaobao
 * @Description:TODO
 * @version:1.0
 */

public class test6 {

    Logger log;

    public static void main(String[] args) {
        int total = 1000000; // 总数量
        BloomFilter<CharSequence> bf = BloomFilter.create(
                Funnels.stringFunnel(Charsets.UTF_8), total, 0.0002
        );
        // 初始化 1000000 条数据到过滤器中
        for (int i = 0; i < total; i++) {
            bf.put("" + i);
        }
        // 判断值是否存在过滤器中
        int count = 0;
        for (int i = 0; i < total + 10000; i++) {
            if (bf.mightContain("" + i)) {
                count++;
            }
        }
        System.out.println("已匹配数量 " + count);
    }

    /**
     * 初始化布隆过滤器
     */
    @Bean
    public BloomFilter bloomInit() {
        // 初始化布隆过滤器，设置数据类型，数组长度和误差值
        BloomFilter<String> bloomFilter = BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), 1000000L, 0.01);
        // 获取要装入过滤器的数据   获取热门的数据
        List<String> hotkey = goodsMapper.getSkillActivityGoods();
        // 循环装填
        for (String key : hotkey) {
            bloomFilter.put("dmhot" + "_" +key);
            log.info("已加入数据到布隆过滤器key:"+key);
        }
        log.info("布隆过滤器装载完成");
        return bloomFilter;
    }

}
